package LeetCode;

import java.util.Stack;

public class LC_071_SimplifyPath {
    public static void main(String[] args) {

    }

    static class Solution {
        public static String simplifyPath(String path) {
            String[] pathArray = path.split("/");
            Stack<String> stack = new Stack<>();
            StringBuilder result = new StringBuilder();

            for (String aPathArray : pathArray) {
                switch (aPathArray) {
                    case "":
                    case ".":
                        break;
                    case "..":
                        if (!stack.isEmpty())
                            stack.pop();
                        break;
                    default:
                        stack.push(aPathArray);
                        break;
                }
            }

            if (stack.isEmpty())
                return "/";
            while (!stack.isEmpty())
                result.insert(0, "/" + stack.pop());

            return result.toString();

        }

    }
}
